<?php
include_once($_SERVER['DOCUMENT_ROOT']."/config.php");
include_once($gDOCUMENT_ROOT_SYSTEM.'include/dao/user.php');
class userAction
{
	private $user;
	public static $GOOGLE_FRIEND_CONNECT_ID = '13496013090209736255';
	public static $GOOGLE_FRIEND_CONNECT_COOKIE_NAME = 'fcauth13496013090209736255';
	public static $GOOGLE_FRIEND_CONNECT_PEOPLE_URL = 'http://www.google.com/friendconnect/api/people/@viewer/@self?fcauth=';
	function __construct()
	{
		$this->user = new user();
		ob_start();
		ini_set('session.use_cookies',1);
		ini_set('session.use_only_cookies',0);
		ini_set('arg_separator.output','&amp;');
		ini_set('session.use_trans_sid',1);
		session_name('sid');
		session_set_cookie_params(0, '/', '.doubanxia.com');
		session_start();
	}
	
	public function addDoubanxiaUser()
	{
		try
		{
			if(isset($_COOKIE[self::$GOOGLE_FRIEND_CONNECT_COOKIE_NAME])&&!empty($_COOKIE[self::$GOOGLE_FRIEND_CONNECT_COOKIE_NAME]))
			{
				$fcauth = $_COOKIE[self::$GOOGLE_FRIEND_CONNECT_COOKIE_NAME];
				$user_json = file_get_contents($GOOGLE_FRIEND_CONNECT_PEOPLE_URL.$fcauth);
				$user_obj = json_decode($user_json);
				if(isset($user_obj->entry->id))
				{
					$power = user::$power;
					$data = array(
						'id' => $user_obj->entry->id,
						'power' => $power['viewer'],
					);
					$result = $this->user->insert($data);
				}
				else
				{
					return false;
				}
			}
			else
			{
				return false;
			}
		}
		catch(Exception $e)
		{
			//echo 'Message: ' .$e->getMessage();
			return false;
		}
		$this->setSession($data);
	}
	
	public function setSession($data)
	{
		$_SESSION['id'] = $data['id'];
		$_SESSION['power'] = $data['power'];
	}
	
	public function isDoubanxiaUser()
	{
		try
		{
			if(isset($_COOKIE[self::$GOOGLE_FRIEND_CONNECT_COOKIE_NAME])&&!empty($_COOKIE[self::$GOOGLE_FRIEND_CONNECT_COOKIE_NAME]))
			{
				$fcauth = $_COOKIE[self::$GOOGLE_FRIEND_CONNECT_COOKIE_NAME];
				$user_json = file_get_contents($GOOGLE_FRIEND_CONNECT_PEOPLE_URL.$fcauth);
				$user_obj = json_decode($user_json);
				if(isset($user_obj->entry->id))
				{
					$where = array(
						'id' => $user_obj->entry->id,
					);
					$column = '*';
					$result = $this->user->select($where,$column);
					if(!empty($result))
					{
						$this->setSession($result);
						return true;
					}
					else
					{
						return false;
					}
				}
				else
				{
					return false;
				}
			}
		}
		catch(Exception $e)
		{
			//echo 'Message: ' .$e->getMessage();
			return false;
		}
		$this->setSession($data);
	}
	
	public function getDoubanxiaUserInfoById($id,$column='*')
	{
		$where = array(
			'id' => $id,
		);
		$result = $this->user->select($where,$column);
		return $result;
	}
	
	public function editDoubanxiaUserInfoById($id,$data)
	{
		$result = $this->user->updateById($id,$data);
		return $result;
	}
	
	public function checkLogin()
	{
		if(isset($_SESSION['id'])&&!empty($_SESSION['id']))
		{
			return true;
		}
		else
		{
			if(isset($_COOKIE[self::$GOOGLE_FRIEND_CONNECT_COOKIE_NAME])&&!empty($_COOKIE[self::$GOOGLE_FRIEND_CONNECT_COOKIE_NAME]))
			{
				$fcauth = $_COOKIE[self::$GOOGLE_FRIEND_CONNECT_COOKIE_NAME];
				$user_json = file_get_contents(self::$GOOGLE_FRIEND_CONNECT_PEOPLE_URL.$fcauth);
				$user_obj = json_decode($user_json);
				if(isset($user_obj->entry->id))
				{
					$where = array(
						'id' => $user_obj->entry->id,
					);
					$column = '*';
					$result = $this->user->select($where,$column);
					if(!empty($result))
					{
						$_SESSION['id'] = $result['id'];
						$_SESSION['power_sum'] = $result['power_sum'];
						return true;
					}
					else
					{
						$power = user::$power;
						$data = array(
							'id' => $user_obj->entry->id,
							'power_sum' => $power['add_film']+$power['add_resource']+$power['view_CN_allowed']+$power['view_CN_inattention'],
						);
						$result = $this->user->insert($data);
						$_SESSION['id'] = $data['id'];
						$_SESSION['power_sum'] = $data['power_sum'];
						return true;
					}
				}
				else
				{
					return false;
				}
			}
			else
			{
				return false;
			}
		}
	}
	
	
	public function isGoogleFriendConnectLogin()
	{
		if(isset($_COOKIE[self::$GOOGLE_FRIEND_CONNECT_COOKIE_NAME])&&!empty($_COOKIE[self::$GOOGLE_FRIEND_CONNECT_COOKIE_NAME]))
		{
			$fcauth = $_COOKIE[self::$GOOGLE_FRIEND_CONNECT_COOKIE_NAME];
			$user_json = file_get_contents($GOOGLE_FRIEND_CONNECT_PEOPLE_URL.$fcauth);
			$user_obj = json_decode($user_json);
			if(isset($user_obj->entry->id))
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			return false;
		}
	}
	
	public function isDoubanxiaLogin()
	{
		if(isset($_SESSION['id'])&&!empty($_SESSION['id']))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public function canIDo($thing)
	{
		if(isset($_SESSION['power_sum']))
		{
			$power = user::$power;
			if(($_SESSION['power_sum']&$power[$thing])>0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			return false;
		}
	}
}
